﻿/*
Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

(function() {
    var audioFilenameRegex = /\.mp3(?:$|\?)/i,
    flashFilenameRegex = /\.swf(?:$|\?)/i,
    numberRegex = /^\d+(?:\.\d+)?$/;

    function cssifyLength(length) {
        if (numberRegex.test(length))
            return length + 'px';
        return length;
    }

    function isAudioEmbed(element) {
        var attributes = element.attributes;
        //return (attributes.type == 'audio/x-ms-wma' || audioFilenameRegex.test(attributes.src || ''));
        return (attributes.type == 'audio/x-ms-wma' || audioFilenameRegex.test(attributes.src) || attributes.classid == 'clsid:22d6f312-b0f6-11d0-94ab-0080c74c7e95');
    }

    function isFlashEmbed(element) {
        var attributes = element.attributes;
        //return (attributes.type == 'application/x-shockwave-flash' || audioFilenameRegex.test(attributes.src || ''));
        return (attributes.type == 'application/x-shockwave-flash' || flashFilenameRegex.test(attributes.src) || attributes.classid == 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000');
    }

    function createAudioFakeElement(editor, realElement) {
        var fakeElement = editor.createFakeParserElement(realElement, 'cke_audio', 'audio', true),
    fakeStyle = fakeElement.attributes.style || '';

        var width = realElement.attributes.width,
    height = realElement.attributes.height;

        if (typeof width != 'undefined')
            fakeStyle = fakeElement.attributes.style = fakeStyle + 'width:' + cssifyLength(width) + ';';

        if (typeof height != 'undefined')
            fakeStyle = fakeElement.attributes.style = fakeStyle + 'height:' + cssifyLength(height) + ';';

        return fakeElement;
    }

    function createFlashFakeElement(editor, realElement) {
        var fakeElement = editor.createFakeParserElement(realElement, 'cke_flash', 'flash', true),
    fakeStyle = fakeElement.attributes.style || '';

        var width = realElement.attributes.width,
    height = realElement.attributes.height;

        if (typeof width != 'undefined')
            fakeStyle = fakeElement.attributes.style = fakeStyle + 'width:' + cssifyLength(width) + ';';

        if (typeof height != 'undefined')
            fakeStyle = fakeElement.attributes.style = fakeStyle + 'height:' + cssifyLength(height) + ';';

        return fakeElement;
    }

    function createObjectFakeElement(editor, realElement) {
        var fakeElement = editor.createFakeParserElement(realElement, 'cke_object', 'object', true),
    fakeStyle = fakeElement.attributes.style || '';

        var width = realElement.attributes.width,
    height = realElement.attributes.height;

        if (typeof width != 'undefined')
            fakeStyle = fakeElement.attributes.style = fakeStyle + 'width:' + cssifyLength(width) + ';';

        if (typeof height != 'undefined')
            fakeStyle = fakeElement.attributes.style = fakeStyle + 'height:' + cssifyLength(height) + ';';

        return fakeElement;
    }

    CKEDITOR.plugins.add('audio',
	{
	    init: function(editor) {
	        editor.addCommand('audioCmd', new CKEDITOR.dialogCommand('audio'));
	        editor.ui.addButton('Audio',
				{
				    label: "Audio",
				    command: 'audioCmd'
				});
	        CKEDITOR.dialog.add('audio', this.path + 'dialogs/audio.js');

	        editor.addCss(
				'img.cke_audio' +
				'{' +
					'background-image: url(' + CKEDITOR.getUrl(this.path + 'images/placeholder.png') + ');' +
					'background-position: center center;' +
					'background-repeat: no-repeat;' +
					'border: 1px solid #a9a9a9;' +
					'width: 80px;' +
					'height: 80px;' +
				'}'
				);
	        editor.addCss(
				'img.cke_object' +
				'{' +
					'background-image: url(' + CKEDITOR.getUrl(this.path + 'images/object.png') + ');' +
					'background-position: center center;' +
					'background-repeat: no-repeat;' +
					'border: 1px solid #a9a9a9;' +
					'width: 80px;' +
					'height: 80px;' +
				'}'
				);
	        /*
	        // If the "menu" plugin is loaded, register the menu items.
	        if ( editor.addMenuItems )
	        {
	        editor.addMenuItems(
	        {
	        audio :
	        {
	        label : editor.lang.flash.properties,
	        command : 'audio',
	        group : 'audio'
	        }
	        });
	        }

			// If the "contextmenu" plugin is loaded, register the listeners.
	        if ( editor.contextMenu )
	        {
	        editor.contextMenu.addListener( function( element, selection )
	        {
	        if ( element && element.is( 'img' ) && element.getAttribute( '_cke_real_element_type' ) == 'audio' )
	        return { audio : CKEDITOR.TRISTATE_OFF };
	        });
	        }
	        */
	    },
	    afterInit: function(editor) {
	        var dataProcessor = editor.dataProcessor,
				dataFilter = dataProcessor && dataProcessor.dataFilter;

	        if (dataFilter) {

	            dataFilter.addRules(
					{
					    elements:
						{
						    'cke:object': function(element) {
						        var attributes = element.attributes, classId = attributes.classid && String(attributes.classid).toLowerCase();
						        if (!classId) {
						            // Look for the inner <embed>
						            for (var i = 0; i < element.children.length; i++) {
						                if (element.children[i].name == 'embed') {
						                    if (isAudioEmbed(element))
						                        return createAudioFakeElement(editor, element);
						                    if (isFlashEmbed(element))
						                        return createFlashFakeElement(editor, element);
						                    return createObjectFakeElement(editor, element);
						                }
						            }
						        }
						        if (isAudioEmbed(element))
						            return createAudioFakeElement(editor, element);
						        if (isFlashEmbed(element))
						            return createFlashFakeElement(editor, element);
						        return createObjectFakeElement(editor, element);
						    },
						    'cke:embed': function(element) {
						        if (isAudioEmbed(element))
						            return createAudioFakeElement(editor, element);
						        if (isFlashEmbed(element))
						            return createFlashFakeElement(editor, element);
						        return createObjectFakeElement(editor, element);
						    }

						}
					},
					999);
	        }
	    },
	    requires: ['fakeobjects']
	});
})();
